<?php

/**
 * This is the model class for table "tbl_messages".
 *
 * The followings are the available columns in table 'tbl_messages':
 * @property integer $message_id
 * @property integer $sender_id
 * @property integer $recipient_id
 * @property string $header
 * @property string $content
 * @property integer $archived
 * @property string $date_created
 * @property integer $is_read
 *
 * The followings are the available model relations:
 * @property TblMember $sender
 * @property TblMember $recipient
 */
class Messages extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Messages the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_messages';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('header, content, date_created', 'required'),
			array('sender_id, recipient_id, archived, is_read', 'numerical', 'integerOnly'=>true),
			array('header', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('message_id, sender_id, recipient_id, header, content, archived, date_created, is_read', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'sender' => array(self::BELONGS_TO, 'TblMember', 'sender_id'),
			'recipient' => array(self::BELONGS_TO, 'TblMember', 'recipient_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'message_id' => 'Message',
			'sender_id' => 'Sender',
			'recipient_id' => 'Recipient',
			'header' => 'Header',
			'content' => 'Content',
			'archived' => 'Archived',
			'date_created' => 'Date Created',
			'is_read' => 'Is Read',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('message_id',$this->message_id);
		$criteria->compare('sender_id',$this->sender_id);
		$criteria->compare('recipient_id',$this->recipient_id);
		$criteria->compare('header',$this->header,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('archived',$this->archived);
		$criteria->compare('date_created',$this->date_created,true);
		$criteria->compare('is_read',$this->is_read);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}